﻿using Astronergy.EFCore.GenericRepository;
using Astronergy.Model.Contexts;
using Astronergy.Model.Dto;
using Astronergy.Model.Models;
using Astronergy.WebApi.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Astronergy.Service
{
    public class SysAreaService
    {
        private readonly IRepository<FrameContext> _frameRepository;
        public SysAreaService(IRepository<FrameContext> frameRepository)
        {
            _frameRepository = frameRepository;
        }

        public async Task<TData<List<SysAreaEntity>>> GetSysAreaListAsync(Pagination pagination)
        {
            TData<List<SysAreaEntity>> obj = new TData<List<SysAreaEntity>>();
            Specification<SysAreaEntity> specification = new Specification<SysAreaEntity>();
            specification.OrderBy = q => q.OrderBy(e => pagination.Sort);
            specification.Skip = pagination.PageIndex;
            specification.Take = pagination.PageSize;
            var data = await _frameRepository.GetListAsync<SysAreaEntity>(specification);
            obj.Code = 1;
            obj.Data = data;
            obj.Message = "获取成功！";
            obj.Total = await _frameRepository.GetLongCountAsync<SysAreaEntity>();
            return obj;
        }

        public async Task<TData<bool>> AddSysAreaAsync(SysAreaEntity entity)
        {
            TData<bool> obj = new TData<bool>();
            await _frameRepository.AddAsync<SysAreaEntity>(entity);
            int result = await _frameRepository.SaveChangesAsync();
            obj.Code = result > 0 ? 1 : 0;
            obj.Data = result > 0 ? true : false;
            obj.Message = result > 0 ? "新增成功！" : "新增失败！";
            return obj; 
        }
    }
}
